from typing import List

from afast_core.core_libs.zyxorm.utils import md5
from infirmary_admin_src.infirmary_common.infirmary_controller import IBaseController
from infirmary_admin_src.infirmary_common.infirmary_controller.rest_controller_with_depends import \
    RestControllerWithDepends
from fastapi import Request, Depends, Path, Body

from infirmary_admin_src.infirmary_smart_admin.infirmary_biz.infirmary.businesses.schemas import SchemaBase
from infirmary_admin_src.infirmary_smart_admin.infirmary_biz.system.employee.services import EmployeeBusinesses



class XParam(SchemaBase):
    '''
    账号密码登录参数
    '''

    employee_id: int = Body(default=1, alias='employeeId', title="employeeId", description='用户ID')
    actual_name: str = Body(None, alias='actualName', title="department_id", description='用户名称')
    administrator_flag: int = Body(default=0, alias='administratorFlag', title="administrator_flag",description='是否是管理员')
    deleted_flag: int = Body(default=0, alias='deletedFlag', title="deleted_flag", description='是否禁用标记')
    department_id: str = Body(None, alias='departmentId', title="departmentId", description='department_id')
    disabled_flag: int = Body(default=0, alias='disabledFlag', title="disabledFlag", description='权限类型')
    gender: int = Body(default=1, alias='gender', title="gender", description='性别')
    login_name: str = Body(None, alias='loginName', title="loginName", description='loginName')
    remark: str = Body(None, alias='remark', title="remark", description='remark')
    phone: str = Body(None, alias='phone', title="phone", description='loginName')
    create_time: str = Body('now()', alias='createTime', title="create_time", description='创建时间')
    update_time: str = Body('now()', alias='updateTime', title="update_time", description='更细时间')
    roleIdList: List[int] = Body(default=[], alias='roleIdList', title="roleIdList", description='性别')


class IController(IBaseController):
    '''
    定制相关约束检测函数逻辑步序
    '''

    def __init__(self, *, request: Request,
                 schema_param: XParam,
                 ):
        super().__init__(request=request)
        # 入参参数
        # 入参参数
        self.schema_param = schema_param


@RestControllerWithDepends()
class SystemEmployeeAddController(IController):

    def business_login(self):
        pass
        # 查询用户信息，联表查询比较麻烦
        #  **{**self.schema_param.dict(exclude_unset=True, exclude={'is_trusted'}),

        # 先判断名称是否重复
        if EmployeeBusinesses.get_sys_employee_info_by_login_name(login_name=self.schema_param.login_name):
            self.response_api_code = 30001
            self.response_ok = False
            self.response_msg = '登入用户名称已存在，请重新输入！'
            return

        login_pwd = {
            # 设置默认的密码
            'login_pwd':md5('zyg123456').lower()
        }
        self.schema_param.employee_id = EmployeeBusinesses.get_sys_employee_id_max_id() + 1
        result = EmployeeBusinesses.add_sys_employee_and_role(
            roleIdLists=self.schema_param.roleIdList,
            **{**self.schema_param.dict(exclude={'roleIdList'}),**login_pwd}
        )
        return 'zyg123456'
